package com.newx.test.config;

import com.newx.file.config.NewxFileConfig;
import com.newx.limit.handler.interceptor.BrushInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author yxl
 * @date 2024/7/25 0025 9:18
 */
@Configuration
public class NewxWebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private NewxFileConfig config;
//    @Autowired
//    private BrushInterceptor brushInterceptor;
//
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 防刷
//        registry.addInterceptor(brushInterceptor);
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /*
            配置server虚拟路径，handler为前台访问的URL目录，locations为files相对应的本地路径
            也就是说如果有一个 upload/avatar/aaa.png 请求，那程序会到后面的目录里面找aaa.png文件
            另外：如果项目中有使用Shiro，则还需要在Shiro里面配置过滤下
         */
        if(System.getProperty("os.name").contains("Windows")){
            registry.addResourceHandler("/"+config.getLocalFilePath().substring(3)+"/**").addResourceLocations("file:"+config.getLocalFilePath()+"/");
        }
        if(System.getProperty("os.name").contains("Linux")){
            registry.addResourceHandler("/"+config.getLocalFilePath().substring(1)+"/**").addResourceLocations("file:"+config.getLocalFilePath()+"/");
        }

    }
}
